package cn.seecoder.courselearning.mapperservice.course;

import cn.seecoder.courselearning.po.course.Course;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CourseMapper {
    
    int deleteByPrimaryKey(Integer id);

    int insert(Course record);

    Course selectByPrimaryKey(Integer id);

    List<Course> selectAll();

    int updateByPrimaryKey(Course record);

    @ResultMap("info")
    @Select("select c.id, c.name, c.type, c.intro, c.picture, c.school, c.create_time, c.delete_time, c.cost, c.teacher_id, c.teacher_name, IF(a.cource_id,1,0) as isRent,count(t.user_id) as likes  " +
            "    from course c  " +
            "    left join course_likes as t  " +
            "    on c.id = t.course_id  " +
            "    left join rent_course a ON c.id=a.cource_id AND a.enabled=1 and a.user_id=#{uid} " +
            "    where type = #{type,jdbcType=VARCHAR}  " +
            "    group by c.id")
    List<Course> selectByType(String type,Integer uid);

    @ResultMap("info")
    @Select("<script>" +
            "select c.id, name, type, intro, picture, school, c.create_time, delete_time, cost, teacher_id, teacher_name, IF(a.cource_id,1,0) as isRent,count(t.user_id) as likes  " +
            "    from course c  " +
            "    left join course_likes as t  " +
            "    on c.id = t.course_id  " +
            "    left join rent_course a ON c.id=a.cource_id AND a.enabled=1 and a.user_id=#{uid}" +
            "    <where>  " +
            "      <if test='key!=null and key.length()>0 '>  " +
            "        name like concat('%',#{key},'%')  " +
            "      </if>  " +
            "    </where>  " +
            "    group by c.id  " +
            "    order by create_time asc" +
            "</script>")
    List<Course> queryAll(String key,Integer uid);

    List<Course> selectByTeacherId(Integer userId);

    List<Course> selectByStudentId(Integer userId);


    @Results(id = "info",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "NAME",property = "name"),
            @Result(column = "type",property = "type"),
            @Result(column = "intro",property = "intro"),
            @Result(column = "picture",property = "picture"),
            @Result(column = "school",property = "school"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "cost",property = "cost"),
            @Result(column = "teacher_id",property = "teacherId"),
            @Result(column = "teacher_name",property = "teacherName"),
            @Result(column = "likes",property = "likes"),
            @Result(column = "start_time",property = "startTime"),
            @Result(column = "end_time",property = "endTime"),
            @Result(column = "rent_id",property = "rentId"),
            @Result(column = "isRent",property = "isRent")
    })
    @Select("select c.id, c.name, c.type, c.intro, c.picture, c.school, c.create_time, c.delete_time, c.cost, c.teacher_id, c.teacher_name, IF(a.cource_id,1,0) as isRent,count(t.user_id) as likes  " +
            "    from course c  " +
            "    left join course_likes t  " +
            "    on c.id = t.course_id  " +
            "    left join rent_course a ON c.id=a.cource_id AND a.enabled=1  and a.user_id=#{uid} " +
            "    group by c.id  " +
            "    order by likes desc"
    )
    List<Course> selectHotCourses(Integer userId);
}